// mircoApp/bn-system/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showBind: false,
    showEwm: false,

    name: '',
    mobile: '',
    code: '',
    timer: null,
    count: 0,

    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initData();
    this.getData();
  },
  initData() {
    this.data.list = [];
    this.data.page = 1;
    this.data.total_page = 1;
    this.data.isLoadMore = true;
  },
  getData() {
    let url = app.getNetAddresss("plugin.bn-system.api.member.get-card-list");
    app._getNetWork({
      url: url,
      data: { test: this.options.test || 0 },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            list: res.data
          });
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {
        console.error(res);
      },
    });
  },
  onClickVal(e){
    let {name,val} = e.currentTarget.dataset;
    this.setData({
      [name]: val
    });
  },
  tobindCard() {
    let { mobile, code, name } = this.data;
    if (app._isTextEmpty(name)) {
      wx.showToast({
        title: "请填写姓名",
        icon: "none",
        duration: 1000,
      });
      return;
    } 
    if (app._isTextEmpty(mobile)) {
      wx.showToast({
        title: "请填写手机号",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (app._isTextEmpty(code)) {
      wx.showToast({
        title: "请填写验证码",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    let url = app.getNetAddresss("plugin.bn-system.api.member.bind-card");
    app._getNetWork({
      url: url,
      data: {
        mobile, code, name, test: this.options.test || 0
      },
      success: (resdata) => {
        let res = resdata.data;
        app.tips(res.msg);
        if (res.result == 1) {
          this.initData();
          this.getData();
          this.setData({
            showBind: false
          });
        }
      },
      fail: function (res) {
        console.error(res);
      },
    });
  },
  // 获取验证码
  VerificationCode() {
    // if (app._isTextEmpty(this.data.form.captcha) && this.data.imgcode) {
    //   wx.showToast({
    //     title: "请填图形验证码",
    //     icon: "none",
    //     duration: 1000,
    //   });
    //   return;
    // }
      if (app._isTextEmpty(this.data.mobile)) {
        wx.showToast({
          title: "请填写手机号",
          icon: "none",
          duration: 1000,
        });
        return;
      } 
      wx.showLoading({
        title: "发送中",
      });
      let urlStr = app.getNetAddresss("plugin.bn-system.api.member.send-code");
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        data: {
          mobile: this.data.mobile,
          // captcha: this.data.form.captcha,
        },
        success: (resdata) => {
          let res = resdata.data;
          wx.hideLoading({
            success: (res) => {},
          });
          if (res.result == 1) {
            app.tips('验证码发送成功');
            this.getCode();
          } else {
            this.setData({
              "form.captcha": "",
            });
            // this.getimgdata();
          }
        },
        fail: function (res) {},
      });
    
  },
  // 60秒倒计时
  getCode() {
    const TIME_COUNT = 60;
    if (!this.data.timer) {
      this.data.count = TIME_COUNT;
      this.data.timer = setInterval(() => {
        if (this.data.count > 0 && this.data.count <= TIME_COUNT) {
          this.data.count--;
          this.setData({
            count:this.data.count
          });
        } else {
          clearInterval(this.data.timer);
          this.data.count = 0;
          this.data.timer = null;
          this.setData({
            count:this.data.count,
            timer:this.data.timer
          });
        }
      }, 1000);
    }
  },
  getEWm(e) {
    let card_no = e.currentTarget.dataset.card_no;
    let url = app.getNetAddresss("plugin.bn-system.api.member.get-qr-code");
    app._getNetWork({
      url: url,
      data: { card_no, test: this.options.test || 0 },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            qrcode: res.data.qrcode,
            showEwm: true
          });
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {
        console.error(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if(this.data.timer) clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if(this.data.timer) clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})